<?php
namespace App\Services;
use EasyWeChat;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Text;

class WechatTpl 
{
	private static $pay_sucess = '11zXzWovcN3iydhtY3ljYbTGnQC1JE1PKbrcg2c9LTJE';
	private static $sended     = 'MyuuLpwiDLLWN17kDIXoB7A7tEyFeg2mfWeySDvC1VM';

	/**
	 * 支付成功通知
	 */
	public static function paySucessNotice($data)
	{
		$app = app('wechat.mini_program');	
		$app->template_message->send([
		    'touser' => 'user-openid',
		    'template_id' => self::$pay_sucess,
		    'page' => 'index',
		    'form_id' => 'form-id',
		    'data' => [
		        'keyword1' => 'VALUE1',  //订单金额
		        'keyword2' => 'VALUE2', //订单号码
		        'keyword3' => 'VALUE3', //商品名称
		        'keyword4' => 'VALUE4', //支付时间
		        'keyword5' => 'VALUE5', //商户名

		        // ...
		    ],
		]);
	}

	/**
	 * 订单发货通知
	 */
	public static function sendNotice($data)
	{
		$app = app('wechat.mini_program');	
		$app->template_message->send([
		    'touser' => $data['openid'],
		    'template_id' => self::$pay_sucess,
		    'page' => 'index',
		    'form_id' => 'form-id',
		    'data' => [
		        'keyword1' => 'VALUE1',  //订单编号
		        'keyword2' => 'VALUE2', //商品名称
		        'keyword3' => 'VALUE3', //发货时间
		        'keyword4' => 'VALUE4', //商家		        
		    ],
		]);    
	}

	/**
	 * 发送红包
	 */
	public static function sendEnvelope($data,$openid)
	{
		return true;

		// 下面的逻辑是发红包的
		$data['code'] = orderCode('E',$data['user_id']);
		$config  = config('wechat.payment.shop');
		$payment = Factory::payment($config);

		$redpack = $payment->redpack;
		$redpackData = [
		    'mch_billno'   => $data['code'],
		    'send_name'    => '活动红包',
		    're_openid'    => $openid,
		    'total_num'    => 1,  //固定为1，可不传
		    'total_amount' => $data['money'],  //单位为分，不小于100
		    'wishing'      => '恭喜发财',		   
		    'act_name'     => '拍十秒',
		    'remark'       => '拍十秒中奖',		    
		];

		$rs = $redpack->sendNormal($redpackData);
		return $rs;
	}

	public static function serviceMsg($msg,$openid)
	{
		$app = app('wechat.mini_program');
		$message = new Text($msg);
		$result = $app->customer_service->message($message)->to($openid)->send();
		return $result;
	}
}